package com.lsh.airticket.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dto.RequestAirroute;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.dto.ShowAirroute;
import com.lsh.airticket.entity.Airroute;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.AirrouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/17
 **/
@RestController
@RequestMapping("/flights")
public class AdminAirrouteController {

    @Autowired
    AirrouteService airrouteService;

    @GetMapping("/admin/airroute/airrouteList")
    public Result findAirrouteList(Integer page) {
        if (page == null || page == 0) {

            page = 1;
        }
        PageHelper.startPage(page, 8);
        // PageInfo<Airroute> pageInfo= airrouteService.findAirrouteList();

        //查找所有的航线
        PageInfo<ShowAirroute> airrouteList = airrouteService.findAirrouteList();
        return Result.result().addData("pageInfo", airrouteList);
    }

    /**
     * 添加一条航线信息
     *
     * @return
     */
    @PostMapping("/admin/airroute/insertAirroute")
    public Result insertAirroute(@RequestBody RequestAirroute requestAirroute) {
        int i = airrouteService.insertTbAirroute(requestAirroute);
        if (i != 1) {
            throw new UserException("插入失败");
        }
        return Result.result().setMsg("插入成功");
    }

    /**
     * 查询 所有的 航线。
     * 和 根据 起点城市 和 终点城市查找 所有的航线信息。
     *
     * @return
     */
    @GetMapping("/admin/airroute/findAirrouteAll")
    public Result findAirrouteAll(Integer fromCityId, Integer toCityId) {
        List<Airroute> airrouteAll = null;
        if (fromCityId == null || toCityId == null) {
            airrouteAll = airrouteService.findAirrouteAll();
        } else {
            airrouteAll = airrouteService.findAirrouteByCity(fromCityId, toCityId);
        }
        return Result.result().addData("airrouteAll", airrouteAll);
    }

    /**
     * 删除一个航线
     *
     * @return
     */
    @GetMapping("/admin/airroute/deleteAirroute")
    public Result deleteAirroute(Integer airrouteId) {
        int i = airrouteService.deleteAirrouteById(airrouteId);
        if (i != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }


    // /**
    //  * 插入一条  航班信息。
    //  * @return
    //  */
    // @PostMapping("/admin/flight/insertFlight")
    // public Result insertFlight(@RequestBody Flight flight) {
    //
    //     return Result.result();
    // }
}
